<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="商户ID">
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item label="币种">
          <el-input v-model="form.currency" placeholder="币种" />
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="form.order_no" placeholder="订单号" />
        </el-form-item>
        <el-form-item label="拒付状态">
          <el-select v-model="form.status" placeholder="拒付状态" clearable>
            <el-option key="0" label="待处理" value="0"></el-option>
            <el-option key="1" label="成功" value="1"></el-option>
            <el-option key="2" label="失败" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" />
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="120"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="200"
          />
          <el-table-column
            prop="order_no"
            label="订单号"
            align="center"
            width="250"
          />
          <el-table-column prop="amount" label="金额" align="center" />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="check_status" label="状态" align="center">
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.check_status == 1
                    ? 'success'
                    : scope.row.check_status == 2
                    ? 'danger'
                    : 'warning'
                "
              >
                {{ scope.row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="dispute_id"
            label="拒付ID号"
            align="center"
            width="250"
          />
          <el-table-column
            prop="dispute_stage"
            label="拒付阶段"
            align="center"
          />
          <el-table-column
            prop="dispute_status"
            label="拒付状态"
            align="center"
          />
          <el-table-column
            prop="due_at"
            label="争议到期日"
            align="center"
            width="180"
          />
          <el-table-column prop="card_brand" label="卡品牌" align="center" />
          <el-table-column
            prop="arn_number"
            label="收单机构参考号"
            align="center"
          />
          <el-table-column prop="issuer_comment" label="评论" align="center" />
          <el-table-column prop="reason" label="拒付原因" align="center" />
          <el-table-column prop="reason_type" label="原因种类" align="center" />
          <el-table-column
            prop="finish_at"
            label="完结时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            prop="createtime"
            label="拒付时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, pickerDefaultTime } from "@/utils/utils";
import { payOrderDisputeApi } from "@/api/ExAssistant/payorder";
const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const formData = {
  merchant_id: "",
  order_no: "",
  currency: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    currency: form.currency,
    status: form.status,
    merchant_id: form.merchant_id,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await payOrderDisputeApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 查询
const onQuery = () => {
  console.log("onQuery");
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
